package conchmusic.conchmusicclient.mainUi;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import android.widget.ImageButton;

import android.widget.TextView;

import java.util.ArrayList;

import conchmusic.conchmusicclient.R;
import conchmusic.conchmusicclient.activity.SongsActivity;
import conchmusic.conchmusicclient.pojo.SongList;
import conchmusic.conchmusicclient.popupWindow.SongListPopu;

/**
 * Created by arsenal_tj on 2017/4/19.
 */

public class songListAdpater extends BaseAdapter {
    //    private ArrayList<HashMap<String, Object>> datalist;
    private ArrayList<SongList> datalist;
    private LayoutInflater inflater;
    private Context context;
    private SurePopup sp;
    private UpdatePopup up;
    private SongListPopu songListPopu;
    private Activity activity;

    public static int getPos() {
        return pos;
    }

    private static int pos;

    public static String getSongListName() {
        return songListName;
    }

    private static String songListName;

    public songListAdpater(Context context, ArrayList<SongList> datalist) {
        this.context = context;
        this.datalist = datalist;
        inflater = LayoutInflater.from(context);
        activity=(Activity)context;
    }

    @Override
    public int getCount() {
        return datalist.size();
    }

    @Override
    public Object getItem(int position) {
        return datalist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        listMain lm = new listMain();
        convertView = inflater.inflate(R.layout.activity_list_main, null);

        lm.imageButton=(ImageButton)convertView.findViewById(R.id.songlist_button);

        lm.songListTv = (TextView) convertView.findViewById(R.id.songListAdpater);
        String slt = datalist.get(position).getSongListName();
        lm.songListTv.setText(slt);
        lm.imageButton.setImageResource(R.drawable.ic_list_item_action);
        lm.songListTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int slistId = datalist.get(position).getSongListId();
                Log.i("tj", "onClick: " + slistId);
                jumpActivity(slistId);
            }
        });
        lm.imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                songListPopu= new SongListPopu(context,position,datalist);
                songListPopu.showAtLocation(activity.findViewById(R.id.content_main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
            }
        });
        return convertView;
    }

    public void jumpActivity(int s) {
        Intent intent = new Intent();
//                intent.putExtra("action","play");
        intent.setClass(context, SongsActivity.class);
        //跳转到歌单页面，并发送歌单名字出去在数据库匹配一下，再显示到那个activity。
        intent.putExtra("slist",s);
        intent.addFlags(intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
}
